package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.ActivityListObject;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.ActivityDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/28.
 */
public class ActivityListAdapter extends RecyclerView.Adapter<ActivityListAdapter.ActivityListHolder> {
    private Context context;
    private List<ActivityListObject.ListBean> list;
    private LayoutInflater layoutInflater;


    public ActivityListAdapter(Context context, List<ActivityListObject.ListBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(List<ActivityListObject.ListBean> list){
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public ActivityListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(R.layout.activity_list_item,parent,false);
        AutoUtils.autoSize(view);
        return new ActivityListHolder(view);
    }

    @Override
    public void onBindViewHolder(ActivityListHolder holder, int position) {
        holder.position=position;
        if(list!=null&&list.size()!=0){
            holder.activity_name.setText("发布者:"+list.get(position).getRealName());
            holder.activity_title.setText(list.get(position).getTitle());
            holder.activity_time.setText(list.get(position).getCreateTime());
            holder.activity_num.setText("报名人数:"+list.get(position).getEnrollment()+"人");
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class ActivityListHolder extends RecyclerView.ViewHolder{
        int position;
        TextView activity_title,activity_classify,activity_num,activity_time,activity_name;
        RelativeLayout activity_item_view;
        public ActivityListHolder(View itemView) {
            super(itemView);
            activity_item_view= (RelativeLayout) itemView.findViewById(R.id.activity_item_view);
            activity_title= (TextView) itemView.findViewById(R.id.activity_title);
            activity_classify= (TextView) itemView.findViewById(R.id.activity_classify);
            activity_num= (TextView) itemView.findViewById(R.id.activity_num);
            activity_time= (TextView) itemView.findViewById(R.id.activity_time);
            activity_name= (TextView) itemView.findViewById(R.id.activity_name);
            activity_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Gson gson=new Gson();
                    String json=gson.toJson(list.get(position));
                    MyMethod.toActivity(context, ActivityDetail.class,new String[]{"json"},new String[]{json});
                }
            });
            activity_item_view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    new AlertDialog.Builder(context).setMessage("是否删除").setNegativeButton("删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            MyOkHttpUtils.getInstance(context).doDetele(Constants.ShouCang2+"/"+list.get(position).getId())
                                    .excute(new MyOkHttpUtils.OKCallBack() {
                                        @Override
                                        public void onFailure(String message) {

                                        }

                                        @Override
                                        public void onResponse(String message) {
                                            Log.i("ActivityListAdapter","message==delete=="+message);
                                            if(message!=null){
                                                try {
                                                    JSONObject jsonObject=new JSONObject(message);
                                                    boolean result=jsonObject.getBoolean("result");
                                                    if(result){
                                                        list.remove(position);
                                                        UpData(list);
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    });
                            dialogInterface.dismiss();
                        }
                    }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).show();
                    return false;
                }
            });
        }
    }
}
